<?php
/***
 *
 * 发送通知消息
 *
 */

namespace App\Http\Services;

use Illuminate\Http\Request;
class MessageServices
{
    private $WebHook = ''; //webhook 地址

    const EXCEPTION_URL_TEST = "https://hooks.slack.com/services/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"; //测试环境
    const EXCEPTION_URL_PROD = "https://hooks.slack.com/services/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"; //生产环境

    public function __construct($WebHook)
    {
        $this-> WebHook = $WebHook;
    }

    /**
     * 发送错误警报
     * @param Request $request
     * @param \Exception $exception
     * @return void
     */
    public static function sendException(Request $request, \Exception $exception)
    {
        //本地环境不报警
        if(env('APP_ENV') == 'local'){
            return;
        }

        //白名单
        if (self::whiteList($request, $exception) == false) {
            return;
        }

        $traceId = time() . rand(1000000, 9999999);
        $date = date("Y-m-d H:i:s");

        //trace信息过大，不发送飞书，写入文件日志，需要查询trace时用traceId查询文件日志
        $log = [
            'traceId' => $traceId,
            'time' => $date,
            'message' => $exception->getMessage(),
            'line' => $exception->getLine(),
            'file' => $exception->getFile(),
            'code' => $exception->getCode(),
            'request' => $request->all(),
            'trace' => $exception->getTraceAsString(),
        ];

        write_log("exceptionLog", 'exceptionTrace', $log);

        //发送到飞书
        $msg = [
            "【花遇派对 Manage服务报错】",
            "[time] " . $date,
            "[message] " . $exception->getMessage(),
            "[line] " . $exception->getLine(),
            "[file] " . $exception->getFile(),
            "[code] " . $exception->getCode(),
            "[request] " . json_encode($request->all(), JSON_UNESCAPED_UNICODE),
            "[traceId] " . $traceId,
        ];

        $content = json_encode([
                'text' => implode("\n",$msg)
            ]);

        try {
            do{
                $noError = [
                    '/data/hypd-manage/vendor/laravel/framework/src/Illuminate/View/Compilers/Compiler.php',
                    'Please provide a valid cache path.','Invalid method override "__CONSTRUCT".',
                    '/data/hypd-manage/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php',
                ];
                if(in_array($exception->getMessage(),$noError) || in_array($exception->getFile(),$noError)){
                    break;
                }
                $url = (env('APP_ENV') != 'production') ? self::EXCEPTION_URL_TEST : self::EXCEPTION_URL_PROD;
                curl_request($url, $content, 'plain');
            }while(false);
            return true;
        } catch (\Exception $e) {
            write_log("exceptionLog", "报警报错", ['url' => $url, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 报警白名单
     * 已知错误禁止报警的写在这里
     *
     * @param Request $request
     * @param \Exception $exception
     * @return bool true报警 false不报警
     */
    private static function whiteList(Request $request, \Exception $exception)
    {
        //$params = $request->all();
        //$path = $params['_url'] ?? "";
        return true;
    }

}
